package com.kkd.kkdapp.patternlock;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.kkd.kkdapp.BaseApplication;
import com.kkd.kkdapp.activity.HomeActivity;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;

public class PatternLockUtils {

	public static final int REQUEST_CODE_CONFIRM_PATTERN = 128;// 19951208;

	public static void setPattern(List<PatternView.Cell> pattern,
			Context context) {
		PreferenceUtils.putString(PreferenceContract.KEY_PATTERN_SHA1,
				PatternUtils.patternToSha1String(pattern), context);
	}

	private static String getPatternSha1(Context context) {
		return PreferenceUtils.getString(PreferenceContract.KEY_PATTERN_SHA1,
				PreferenceContract.DEFAULT_PATTERN_SHA1, context);
	}

	public static boolean hasPattern(Context context) {
		return !TextUtils.isEmpty(getPatternSha1(context));
	}

	public static boolean isPatternCorrect(List<PatternView.Cell> pattern,
			Context context) {
		return TextUtils.equals(PatternUtils.patternToSha1String(pattern),
				getPatternSha1(context));
	}

	public static void clearPattern(Context context) {
		PreferenceUtils.remove(PreferenceContract.KEY_PATTERN_SHA1, context);
	}

	public static boolean checkConfirmPatternResult(Activity activity,
			int requestCode, int resultCode, boolean finish) {
		if (requestCode == REQUEST_CODE_CONFIRM_PATTERN
				&& resultCode != Activity.RESULT_OK) {
			if (finish) {
				activity.startActivity(new Intent(activity, HomeActivity.class));
				activity.finish();
			}
			return false;
		} else {
			return true;
		}
	}

}
